<?php

namespace app\pc\controller;

use think\Db;
use model\PolicyModel;
class User extends CommonController
{
    const ROLE_COMPANY = 1;         // 保险公司人员
    const ROLE_CODER = 2;           // 代录人
    const ROLE_PROTECT = 3;         // 被保险人
    const ROLE_SALE_MAN = 4;        // 业务员

    public $request;

    public function __construct(\think\Request $request)
    {
        parent::__construct($request);
        $this->request = $request->param();
        if($this->uid){
            $user = Db::table('user_info')->where('ui_id',$this->uid)->find();
            $this->assign('user',$user);
            $this->assign('menu_name','个人信息');
        }
    }


    /**
     * 所有角色查询个人信息（通用）
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function user_info()
    {
        $user_id = $this->uid;
        $res = Db::table('user_info')->where('ui_id',$user_id)->find();
        switch ($res['role_id']){
            case self::ROLE_COMPANY:
                $field = [
                    'user_info.head_img',
                    'user_info.role_id',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                    'user_info.card_num',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                break;
            case self::ROLE_CODER:
                $field = [
                    'user_info.head_img',
                    'user_info.role_id',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                    'user_info.card_num',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                $a = Db::table('user_code_r')->where('recoder_id',$user_id)->find();
                $user = Db::table('user_info')->where('ui_id',$a['user_id'])->find();
                $data['company_man'] = $user['username'];
                $data['company_linkman'] = $user['mobile'];
                break;
            case self::ROLE_PROTECT:
                $field = [
                    'user_info.head_img',
                    'user_info.role_id',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                break;
            case self::ROLE_SALE_MAN:
                $field = [
                    'user_info.head_img',
                    'user_info.role_id',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                    'user_info.card_num',
                    'user_info.sale_man_company',
                    'user_info.sale_man_linkman',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                break;
        }
        $this->assign('info',$data);

        return $this->fetch('user/user_info');
    }

    /**
     * 个人中心
     * @return \think\response\View
     */
    public function person()
    {
        $info = Db::table('user_info')->where('ui_id',$this->uid)->find();
        $this->assign('info',$info);
        return view();
    }

    public function message()
    {
        $info = Db::table('message_notice')->where('ui_id',$this->uid)->select();
        $this->assign('info',$info);
        return $this->fetch('user/message');
    }

    /**
     * 保险人个人中心
     * @return \think\response\View
     */
    public function insurer_info()
    {
        $strArr=[PolicyModel::POLICY_REVISE_STATUS,PolicyModel::POLICY_NOT_LOOK_STATUS,
            PolicyModel::POLICY_RECORDOR_NOT_USED_STATUS,PolicyModel::POLICY_ECORDO_NOT_CONFITM_STATUS,PolicyModel::POLICY_CHECK_PENDING_STATUS,PolicyModel::POLICY_HOLDEL_NOT_CONFITM_STATUS,PolicyModel::POLICY_HOLDEL_NOT_ENTRUST_STATUS,
            PolicyModel::POLICY_COMPLETE_STATUS,PolicyModel::POLICY_AUCTION_COMPLETE_STATUS];
        $list=[];
        foreach ($strArr  as $v){
            $count= Db::table('policy')->where('ui_id',$this->uid)->where('delete_ts',0)->where('policy_status',$v)->count();
            $data = Db::table('policy')->where('ui_id',$this->uid)->where('delete_ts',0)->where('policy_status',$v)->select();
            $list[$v]['count']=$count?$count:0;
            $list[$v]['data']=$data?$data:[];
        }
        //获取信息
        return $this->fetch('user/insurer_info',['list'=>$list]);
    }

    /**
     * 被保险人个人中心
     * @return \think\response\View
     */
    public function rem_info()
    {
        return $this->fetch('user/rem_info');
    }

}
